//
//  ViewController.m
//  11102
//
//  Created by student on 16/3/5.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageView;

@property (weak, nonatomic) IBOutlet UITableView *tabelView;

@property (nonatomic,strong) NSArray *picArr;

@property (nonatomic,strong) NSArray *nameArr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.picArr = @[@"1",@"2",@"3",@"4",@"5",@"6"];
    self.nameArr = @[@"南极",@"桂林",@"上海",@"未来",@"大海",@"马拉尼"];
    self.scrollView.delegate = self;
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [self setUpScrollView];
    [self setUpPageView];
}


-(void)setUpScrollView{
    for (int i = 0; i < self.picArr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*[UIScreen mainScreen].bounds.size.width, self.scrollView.bounds.origin.y, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:self.picArr[i]]];
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width*6, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
}

-(void)setUpPageView{
    self.pageView.numberOfPages = self.picArr.count;
    self.pageView.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageView.pageIndicatorTintColor = [UIColor yellowColor];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageView.currentPage = scrollView.contentOffset.x/self.scrollView.frame.size.width;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = self.nameArr[indexPath.row];
    
    return cell;
}
//选中cell 图片和pageView进行切换
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.pageView.currentPage = indexPath.row;
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width*indexPath.row,0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
